package com.zzl.bonds.ticket.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.zzl.bonds.ticket.constant.GotoTypeEnum;
import com.zzl.bonds.ticket.constant.MailMessageConstant;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.dto.common.MsgInfoDto;
import com.zzl.bonds.ticket.dto.feign.BathMsgDto;
import com.zzl.bonds.ticket.entity.TicketFinanceEntity;
import com.zzl.bonds.ticket.entity.TicketOpenEntity;
import com.zzl.bonds.ticket.service.common.MailMessageService;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 站内信内容
 *
 * @author gull
 * @date 2021/12/7 10:16:58
 */
@Service
@Slf4j
public class MailMessageServiceImpl implements MailMessageService {
    /**
     * 开立发消息,发送单个消息给复核人，调用的方法
     *
     * @return 消息内容
     */
    @Override
    public MsgInfoDto setTicketOpenMsg(TicketOpenEntity entity, User user) {
        //发送消息：1、提交开立申请，2、拉起签收流程
        MsgInfoDto msgInfoDto = getMsgInfoDto(entity
                , MailMessageConstant.TICKET_OPEN_APPROVE
                        .replace(MailMessageConstant.TICKET_REPLACE_APPLY, user.getUsername()))
                ;

        return msgInfoDto;
    }

    /**
     * 开立发消息,发送多个消息时，调用的方法
     *
     * @return 消息内容
     */
    @Override
    public List<BathMsgDto> setBatchTicketOpenMsg(TicketOpenEntity entity, User user) {
        Integer ticketStatus = entity.getTicketStatus();
        List<BathMsgDto> bathMsgDtoList = new ArrayList<>();
        switch (ticketStatus) {
//            1、内部复核通过，平台审批中  todo 因为状态暂时设为签收中，所以此处只能先设置3
//            case 2: {
            case 3: {
                //1.1 申请人消息
                MsgInfoDto msgInfoDto = getMsgInfoDto(entity, MailMessageConstant.TICKET_OPEN_PASS_APPLICANT_MSG);
                //申请人发送的消息对象
                BathMsgDto bathMsgDto = new BathMsgDto(JSON.toJSONString(msgInfoDto), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto);
                //1.2 平台审核人员消息
                MsgInfoDto msgInfoDto1 = getMsgInfoDto(entity, MailMessageConstant.TICKET_OPEN_PASS_PLAT_MSG);
                //todo 平台审核人员
                BathMsgDto bathMsgDto1 = new BathMsgDto(JSON.toJSONString(msgInfoDto1), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto1);
            }
            //2、复核未通过
            case 12: {
                //1.1 申请人消息
                MsgInfoDto msgInfoDto = getMsgInfoDto(entity, MailMessageConstant.TICKET_OPEN_FAIL_APPLICANT_MSG);
                //申请人发送的消息对象
                BathMsgDto bathMsgDto = new BathMsgDto(JSON.toJSONString(msgInfoDto), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto);
                //1.2 抄送人 ---》改为流程服务发送抄送人消息
            }

        }
        return bathMsgDtoList;
    }

    /**
     * 融资发消息,发送单个消息给复核人时，调用的方法
     *
     * @return 消息内容
     */
    @Override
    public MsgInfoDto setTicketFinanceMsg(TicketFinanceEntity ticketFinanceEntity, User user) {
        //发送消息：1、提交融资申请，2、拉起银行签收流程
        MsgInfoDto msgInfoDto = getFinanceMsgDto(ticketFinanceEntity
                , MailMessageConstant.TICKET_FINANCE_APPROVE
                        .replace(MailMessageConstant.TICKET_REPLACE_APPLY, user.getUsername())
        );
        return msgInfoDto;
    }

    /**
     * 融资发消息,发送多个消息时，调用的方法
     *
     * @return 消息内容
     */
    @Override
    public List<BathMsgDto> setBatchTicketFinanceMsg(TicketFinanceEntity entity, User user) {
        Integer ticketStatus = entity.getTicketStatus();
        List<BathMsgDto> bathMsgDtoList = new ArrayList<>();
        switch (ticketStatus) {
//            1、内部复核通过，平台审批中
            case 10: {
                //1.1 申请人消息
                MsgInfoDto msgInfoDto = getFinanceMsgDto(entity, MailMessageConstant.TICKET_FINANCE_PASS_APPLICANT_REVIEW);
                //申请人发送的消息对象
                BathMsgDto bathMsgDto = new BathMsgDto(JSON.toJSONString(msgInfoDto), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto);
                //1.2 平台审核人员消息
                MsgInfoDto msgInfoDto1 = getFinanceMsgDto(entity, MailMessageConstant.TICKET_FINANCE_PASS_PALT_REVIEW);
                //todo 平台审核人员
                BathMsgDto bathMsgDto1 = new BathMsgDto(JSON.toJSONString(msgInfoDto1), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto1);
            }
            //2、复核未通过
            case 12: {
                //1.1 申请人消息
                MsgInfoDto msgInfoDto = getFinanceMsgDto(entity, MailMessageConstant.TICKET_FINANCE_FAIL_MSG);
                //申请人发送的消息对象
                BathMsgDto bathMsgDto = new BathMsgDto(JSON.toJSONString(msgInfoDto), entity.getCreatorId().toString(), user);
                bathMsgDtoList.add(bathMsgDto);
                //1.2 抄送人 ---》改为流程服务发送抄送人消息
            }

        }
        return bathMsgDtoList;
    }

    /**
     * 获取融资发送消息的实体
     *
     * @param ticketFinanceEntity 融资申请
     * @param msg                 消息内容
     * @return
     */
    public MsgInfoDto getFinanceMsgDto(TicketFinanceEntity ticketFinanceEntity, String msg) {
        msg = this.getFinanceMsg(ticketFinanceEntity, msg);
        return new MsgInfoDto(ticketFinanceEntity.getId(), GotoTypeEnum.GOTO_TYPE_FINANCE.getCode().toString(), msg, null);
    }

    /**
     * 获取开立发送消息的实体
     *
     * @param entity 开立申请
     * @param msg    消息内容
     * @return
     */
    private MsgInfoDto getMsgInfoDto(TicketOpenEntity entity, String msg) {
        msg = this.getMsgInfo(entity, msg);
        return new MsgInfoDto(entity.getId(), GotoTypeEnum.GOTO_TYPE_OPEN.getCode().toString(), msg, null);
    }

    /**
     * 获取融资发送消息的实体
     *
     * @param ticketFinanceEntity 融资申请
     * @param msg                 消息内容
     * @return
     */
    @Override
    public String getFinanceMsg(TicketFinanceEntity ticketFinanceEntity, String msg) {
        return msg
                .replace(MailMessageConstant.TICKET_REPLACE_INSTITUTY, ticketFinanceEntity.getInstinctyName())
                .replace(MailMessageConstant.TICKET_REPLACE_MONEY, NumberUtil.numberFormatString(ticketFinanceEntity.getTicketAmount()))
                .replace(MailMessageConstant.TICKET_REPLACE_OPEN, ticketFinanceEntity.getOpenEnterpriseName());

    }

    /**
     * 获取开立发送消息的实体
     *
     * @param entity 开立申请
     * @param msg    消息内容
     * @return
     */
    @Override
    public String getMsgInfo(TicketOpenEntity entity, String msg) {
        return msg
                .replace(MailMessageConstant.TICKET_REPLACE_SIGN, entity.getReceiveEnterpriseName())
                .replace(MailMessageConstant.TICKET_REPLACE_MONEY, NumberUtil.numberFormatString(entity.getTicketAmount()))
                .replace(MailMessageConstant.TICKET_REPLACE_OPEN, entity.getEnterpriseName())
                .replace(MailMessageConstant.TICKET_REPLACE_APPLY, entity.getCreatorName())
                ;
    }
}
